package bank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyMoney extends JPanel implements ActionListener {
    private Atm frame;
    private Icon btnOff = Util.getIcon("/images/buttonOff.jpg");
    private Icon btnSure = Util.getIcon("/images/buttonSure.jpg");
    private JButton imageBtn1 = new JButton(btnOff);
    private JButton imageBtn2 = new JButton(btnSure);
    private JTextField newpsw = new JTextField();
    private JTextField surenew = new JTextField();
    public ModifyMoney(Atm frame){
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);


        JLabel acc1 = new JLabel("请输入新密码:");
        acc1.setFont(new Font("宋体", Font.PLAIN, 20));
        acc1.setBounds(200, 200, 300, 50);
        this.add(acc1);
        JLabel acc = new JLabel("请确认新密码:");
        acc.setFont(new Font("宋体", Font.PLAIN, 20));
        acc.setBounds(200, 300, 300, 50);
        this.add(acc);

        newpsw.setBounds(400, 200, 300, 50);
        newpsw.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(newpsw);
        surenew.setBounds(400, 300, 300, 50);
        surenew.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(surenew);




        imageBtn1.setBounds(650, 430, 140, 50);
        this.add(imageBtn1);
        imageBtn1.addActionListener(this);
        imageBtn2.setBounds(300, 430, 140, 50);
        this.add(imageBtn2);
        imageBtn2.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == imageBtn1) {
            frame.switchPanel("menu");
        }else if (e.getSource() == imageBtn2){

            if ( ! newpsw.getText().matches("^[0-9]{6}$")){
                JOptionPane.showMessageDialog(this, "密码不符合要求");
                return;
            }

            if (newpsw.getText().equals(surenew.getText())){
                frame.getUsers().get(frame.getIndex()).setPsw(newpsw.getText());
                JOptionPane.showMessageDialog(this, "修改密码成功");
            }else {
                JOptionPane.showMessageDialog(this, "两次密码不一致，重新输入");
            }

        }
    }
}
